package util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import vo.ApprovalVO;
import vo.TicketVO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

public class PDFCreateUtil {

    /**********************************公共作业票组件****************************************************/
    public static Paragraph createTitle(Font titleFont, String content) throws IOException, DocumentException {
        Paragraph paragraph = new Paragraph(content, titleFont);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingBefore(30f); //设置段落上空白
        paragraph.setSpacingAfter(10f); //设置段落下空白
        return paragraph;
    }

    public static PdfPCell createLogoCell(int tableWidth, int columnNumber, Font tableFont, String numberValue) throws IOException, BadElementException {
        PdfPTable pdfPTable = createLogoTable(tableWidth, tableFont, numberValue);
        PdfPCell cell = new PdfPCell(pdfPTable);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(columnNumber);
        cell.disableBorderSide(Rectangle.LEFT);
        cell.disableBorderSide(Rectangle.RIGHT);
        cell.disableBorderSide(Rectangle.TOP);
        cell.disableBorderSide(Rectangle.BOTTOM);
        return cell;
    }

    public static PdfPTable createLogoTable(int tableWidth, Font tableFont, String numberValue) throws IOException, BadElementException {
        PdfPTable table = new PdfPTable(new float[]{tableWidth / 2, tableWidth / 2});
        //合盛logo样式
        Image image = PDFConst.getHoshine_image();
        image.scalePercent(70);
        PdfPCell cell_1 = createCell(image,false);
        cell_1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell_1.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell_1.setPaddingBottom(2 * tableFont.getSize() / 3);
        cell_1.disableBorderSide(Rectangle.LEFT);
        cell_1.disableBorderSide(Rectangle.RIGHT);
        cell_1.disableBorderSide(Rectangle.TOP);
        cell_1.disableBorderSide(Rectangle.BOTTOM);
        //编号样式
        PdfPCell cell_2 = createCell(tableFont, "编号：" + numberValue);
        cell_2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell_2.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell_2.setPaddingRight(25);
        cell_2.setPaddingBottom(2 * tableFont.getSize() / 3);
        cell_2.disableBorderSide(Rectangle.LEFT);
        cell_2.disableBorderSide(Rectangle.RIGHT);
        cell_2.disableBorderSide(Rectangle.TOP);
        cell_2.disableBorderSide(Rectangle.BOTTOM);

        table.addCell(cell_1);
        table.addCell(cell_2);
        return table;
    }

    public static PdfPTable createSafeMeasureTable(int tableWidth) {
        PdfPTable table = new PdfPTable(new float[]{40,340,50,50});
        table.setTotalWidth(tableWidth);
        table.setLockedWidth(true);
        Font tableFont = PDFConst.getSimhei_nomal_9();

        table.addCell(PDFCreateUtil.createCell(tableFont, "序号"));
        table.addCell(PDFCreateUtil.createCell(tableFont, "安全措施"));
        table.addCell(PDFCreateUtil.createCell(tableFont, "是否涉及"));
        table.addCell(PDFCreateUtil.createCell(tableFont, "确认人"));
        for (int i = 0; i < 28; i++) {
            table.addCell(PDFCreateUtil.createCell(tableFont, ""));
        }
        return table;
    }

    public static java.util.List<PdfPTable> createSignTableList(int tableWidth, int titleWidth, int height, Font tableFont, TicketVO ticketVO) throws IOException, BadElementException {
        java.util.List<PdfPTable> tableList = new ArrayList<>();
        for (ApprovalVO approvalVO:ticketVO.getApprovalList()) {
            tableList.add(PDFCreateUtil.createSignTable(tableWidth, titleWidth, height, tableFont, approvalVO.getDesc(), approvalVO.getContent(), approvalVO.getSignImage(), approvalVO.getDate()));
        }
        return tableList;
    }


    public static PdfPTable createSignTable(int tableWidth, int titleWidth, int height, Font tableFont, String titleValue, String contentValue, String imageValue, String dateValue) throws IOException, BadElementException {
        int dateWidth = tableWidth / 6;
        int signWrapperWidth = dateWidth;
        int signWidth = 40;
        int signImageWidth = signWrapperWidth - signWidth;
        int contentWidth = tableWidth - titleWidth - signWrapperWidth - dateWidth;

        PdfPTable table = new PdfPTable(new float[]{titleWidth, contentWidth, signWidth, signImageWidth, dateWidth});
        table.setTotalWidth(tableWidth);
        table.setLockedWidth(true);

        //标题样式（“xxx意见”）
        PdfPCell cell_1 = createCell(tableFont, titleValue);
        cell_1.setFixedHeight(height);
        cell_1.setVerticalAlignment(Element.ALIGN_TOP);
        cell_1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell_1.disableBorderSide(Rectangle.RIGHT);
        cell_1.setPaddingTop(tableFont.getSize());
        cell_1.setPaddingLeft(2 * tableFont.getSize() / 3);
        //意见内容样式
        PdfPCell cell_2 = createCell(tableFont, contentValue);
        cell_2.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell_2.disableBorderSide(Rectangle.LEFT);
        cell_2.disableBorderSide(Rectangle.RIGHT);
        cell_2.setPaddingBottom(tableFont.getSize());
        //“签字”样式
        PdfPCell cell_3 = createCell(tableFont, "签字：");
        cell_3.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell_3.disableBorderSide(Rectangle.LEFT);
        cell_3.disableBorderSide(Rectangle.RIGHT);
        cell_3.setPaddingBottom(tableFont.getSize());
        //签字图片样式
        Image image = Image.getInstance(imageValue);
        image.scaleAbsolute(height - 5,height - 5);//自定义大小
        PdfPCell cell_4 = createCell(image,false);
        cell_4.disableBorderSide(Rectangle.LEFT);
        cell_4.disableBorderSide(Rectangle.RIGHT);
        //日期样式
        PdfPCell cell_5 = createCell(tableFont, dateValue);
        cell_5.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell_5.disableBorderSide(Rectangle.LEFT);
        cell_5.setPaddingBottom(tableFont.getSize());

        table.addCell(cell_1);
        table.addCell(cell_2);
        table.addCell(cell_3);
        table.addCell(cell_4);
        table.addCell(cell_5);
        return table;
    }

    /**********************************特有作业票组件****************************************************/
    public static PdfPTable createDHZYGasTable(int tableWidth) {
        int firstColumnWidth = 105;
        int otherColumnWidth = (tableWidth - firstColumnWidth) / 6;
        PdfPTable table = new PdfPTable(new float[]{firstColumnWidth, otherColumnWidth, otherColumnWidth, otherColumnWidth, otherColumnWidth, otherColumnWidth, otherColumnWidth});
        table.setTotalWidth(tableWidth);
        table.setLockedWidth(true);
        Font tableFont = PDFConst.getSimhei_nomal_9();

        for (int i = 0; i < 28; i++) {
            table.addCell(PDFCreateUtil.createCell(tableFont, ""));
        }

        PdfPCell cell_0 = table.getRow(0).getCells()[0];
        cell_0.setPhrase(new Phrase("气体取样分析时间", tableFont));

        PdfPCell cell_1 = table.getRow(1).getCells()[0];
        cell_1.setPhrase(new Phrase("代表性气体", tableFont));

        PdfPCell cell_2 = table.getRow(2).getCells()[0];
        cell_2.setPhrase(new Phrase("分析结果", tableFont));


        PdfPCell cell_3 = table.getRow(3).getCells()[0];
        cell_3.setPhrase(new Phrase("分析人", tableFont));
        return table;
    }
    /**********************************单元格组件****************************************************/
    /**
     * 普通单元格
     */
    public static PdfPCell createCell(Font tableFont, String value) {
        return createCell(tableFont, 1,1,PDFConst.getCell_height(),value);
    }
    /**
     * 图片单元格
     * @param image 图片
     * @param fit 设置false，可调整图片大小;设置true，自适应填充单元格,无法调整大小;
     */
    public static PdfPCell createCell(Image image,boolean fit) {
        PdfPCell defaultCell = new PdfPCell(image,fit);
        defaultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        defaultCell.setFixedHeight(PDFConst.getCell_height());
        return defaultCell;
    }
    /**
     * 嵌套表格单元格
     */
    public static PdfPCell createCell(PdfPTable table,int colspan) {
        PdfPCell defaultCell = new PdfPCell(table);
        defaultCell.setColspan(colspan);
        return defaultCell;
    }
    /**
     * 合并单元格
     */
    public static PdfPCell createCell(Font tableFont, int colspan,int rowspan,int height,String value) {
        PdfPCell defaultCell = new PdfPCell();
        defaultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        defaultCell.setPhrase(new Phrase(value, tableFont));
        defaultCell.setFixedHeight(height);
        defaultCell.setColspan(colspan);
        defaultCell.setRowspan(rowspan);
        defaultCell.setBorderWidth(1f);
        return defaultCell;
    }
}
